package com.example.face.mapper.Impl;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import com.example.face.mapper.FaceMapper;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;

/**
 * 人脸识别实现类
 */
public class FaceMapperImpl implements FaceMapper {

    /**
     *
     * @param userId  注意：这个userId是会出现在json字符串的
     * @param image web传来的图片
     * @return
     */
    @Override
    public Boolean faceRegister(String userId, String image) {
        //获得远程服务器的api
        AipFace client=new AipFace("23603795","TwdxGyvi7ZaG7aoxiHzBvOR4","50KWbOz6mGcSFt8i104KDzwvZfK5glOC");
        //options里存放着对人脸的选项
        HashMap<String,String >options=new HashMap<>();
        //精度控制
        options.put("quality_control", "NORMAL");
        //活体检测
        options.put("liveness_control", "NORMAL");

        //往人脸库中添加人脸
        JSONObject res=client.addUser(image,"BASE64","face",userId,options);

        Integer errorCode=res.getInt("error_code");//返回的错误码

        return errorCode==0?true:false;//返回结果error_code=“0”表示成功

    }

    /**
     * 人脸对比检查是否是本人
     * @param image  安卓端传来的图片
     * @return
     */
    @Override
    public Boolean faceCheck(String image) {
        //调用人脸检测
        AipFace client=new AipFace("23603795","TwdxGyvi7ZaG7aoxiHzBvOR4","50KWbOz6mGcSFt8i104KDzwvZfK5glOC");
        HashMap<String,String >options=new HashMap<>();
        options.put("quality_control", "NORMAL");
        //活体检测
        options.put("liveness_control", "NORMAL");

        JSONObject  res=client.detect(image, "BASE64", null);

        if(res.has("error_code")&&res.getInt("erroe_code")==0) {

            JSONObject resultObject=res.getJSONObject("result");

            Integer faceNum=resultObject.getInt("face_num");//如果找的图片是一张，则说明人脸对比成功

            return faceNum==1?true:false;
        }else {
            return false;
        }
    }

    @Override
    public String faceSearch(String image) {

        AipFace client=new AipFace("23603795","TwdxGyvi7ZaG7aoxiHzBvOR4","50KWbOz6mGcSFt8i104KDzwvZfK5glOC");
        HashMap<String,String >options=new HashMap<>();
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "NORMAL");


        JSONObject  res=client.search(image, "BASE64", "face", null);
        System.out.println(res);

        if(res.has("error_msg")&&res.getInt("error_code")==0) {


            JSONObject resultObject=res.getJSONObject("result");
            JSONArray userList=resultObject.getJSONArray("user_list");

            if(userList.length()>0) {
                JSONObject user=userList.getJSONObject(0);//第一个人脸是人脸识别相似度最高的人脸

                double score=user.getDouble("score");
                if(score>80) {
                    return user.getString("user_id");
                }
            }

        }
        return null;

    }

    @Override
    public Boolean faceUpdate(String userId, String image) {

        AipFace client=new AipFace("23603795","TwdxGyvi7ZaG7aoxiHzBvOR4","50KWbOz6mGcSFt8i104KDzwvZfK5glOC");
        HashMap<String,String >options=new HashMap<>();
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "NORMAL");

        JSONObject res = client.updateUser(image, "BASE64", "face", "1000", options);

        Integer errorCode = res.getInt("error_code");
        return errorCode == 0 ? true : false;

    }
}
